package com.nl.sevenuser;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import com.nl.util.AES;
import com.nl.util.Common;
import com.nl.util.ConnectionManager;

public class LoadSend extends Thread {
	private static final Logger logger = Logger.getLogger(LoadSend.class);
	int count1=0;
	private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	static Date d  = new  Date();
	private static final String today = sdf.format(d);
	@Override
	public void run(){
		while (true) {
			
			try {
				checkSend();
				Thread.sleep(30 * 60 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	private void checkSend(){
		logger.info("*********checkSend start***********");
		Calendar calendar=Calendar.getInstance();
		Calendar calendar1=Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY,-24);
		calendar1.set(Calendar.HOUR_OF_DAY,-120);
		String yesterday=sdf.format(calendar.getTime());
		String sixDayAgo = sdf.format(calendar1.getTime());
		Connection conn = null;
		ResultSet rs = null;
		//		PreparedStatement pres = null;
		Statement stmt = null;
		StringBuffer sql = new StringBuffer();
		sql.append("select sum(1) s from boss_sm_send t where t.create_date between ")
		.append("	       to_date('"+today+" 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and" )
		.append("	       to_date('"+today+" 23:59:59', 'yyyy-mm-dd hh24:mi:ss')  and  t.region_id is not null and exists (select * from  boss_sm_send b where  create_date between ")
		.append("	       to_date('"+sixDayAgo+" 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and ")
		.append("	       to_date('"+yesterday+" 23:59:59', 'yyyy-mm-dd hh24:mi:ss')  and b.region_id is not null and b.receiver=t.receiver and b.mgr_id=t.mgr_id )");
				
		try {			
			logger.info(sql.toString());
			conn = ConnectionManager.getInstance().getConnection();							
			stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
					ResultSet.CONCUR_READ_ONLY);
			stmt.setFetchSize(300);
			rs = stmt.executeQuery(sql.toString());
			while(rs.next()){				
				String s = rs.getString("s");
				if (null != s && "".equals(s)) {
					Common.sendWarnSms("今日待发重复数据总量为:"+s);
				}
				
			}	
		} catch (Exception e) {
			logger.info("check smsend error");
			e.printStackTrace();
		} finally {
			try {
				if (null != rs) {
					rs.close();
					rs = null;
				}
				if (null != stmt) {
					stmt.close();
				}
			} catch (SQLException e) {

				e.printStackTrace();
			}
			DbUtils.closeQuietly(conn, null, null);
			
		}	
	}
//	15298652598   BUZuK_yRBCQT57qZIUGXnw
//	15895099536   PHkirQkinbHABMJRximg4w
//	13401810663   jFEgHDsZxcaqDnltS5FfnQ
//	13852395319   8D5Qp2qCB8F8VPC_SMiKpQ
//	13776735005   SOvV5i2nXxme-G2qOLB1hQ
//	19851760016   9c7ZbAjsdCCFkYquZMjL6w
//	18852372190   o1DfipmQ_FCqEF5Ws6Nu3g
//	19851762958   N8xHnRDya-nlhu1q2ocYKA
//	19851762048   uo6Gg6Bl7qYneir1nDUeoQ
//	13952395258  6ZvoY3aCiibbfpsm3euLEQ
	
	public static void main(String[] args) throws Exception {
		String key = "HuaDongJieSuo123";
        String str = "13952395258";
        String en = AES.encrypt(str, key);
        System.out.println(en);
	}
}
